using MediatR;
using ThreeTeams.API.Application.Command.BoxTypeManageMentCommand;
using ThreeTeams.Domain;
using ThreeTeams.ErrorCode;
using ThreeTeams.Infrastructure;

namespace ThreeTeams.API.Application.CommandHandler.BoxTypeManageMentCommandHandler
{
    public class DeleteBoxTypeCommandHandler : IRequestHandler<DeleteBoxTypeCommand, ApiResult<int>>
    {
        private readonly IBaseRepository<BoxType> baseRepository;

        public DeleteBoxTypeCommandHandler(IBaseRepository<BoxType> baseRepository)
        {
            this.baseRepository = baseRepository;
        }

        public async Task<ApiResult<int>> Handle(DeleteBoxTypeCommand request, CancellationToken cancellationToken)
        {
            var result = new ApiResult<int>();
            var id = request.Ids.Split(',').Select(int.Parse).ToList();
            var entity = await baseRepository.DeletedRange(p => id.Contains(p.Id));
            if (entity > 0)
            {
                result.Code = ApiEnum.Ok;
                result.Message = "删除成功";
            }
            else
            {
                result.Code = ApiEnum.Fail;
                result.Message = "删除失败";
            }
            return result;
        }
    }
} 